<?php
namespace app\admin\controller;
use think\Request;
use app\base\model\{
    Order,
    OrderGoods
};
class Orders extends Common{
    //订单模型
    protected $order;
    //订单商品模型
    protected $orderGoods;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->order = new Order();
        $this->orderGoods = new OrderGoods();
    }
    /**
     *获取订单列表
     */
  public function getOrdersList(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $pageNo = $this->request->post('pageNo',1);
      $pageSize = $this->request->post('pageSize',10);
      $keywords = $this->request->post('keywords');
      if($keywords){
          $where['o.order_sn']=['like',"%$keywords%"];
          $whereOr['u.user_name']=['like',"$keywords"];
      }
      $where['o.is_delete']=0;
      $whereOr['o.is_delete']=0;
      $list = $this->order->alias('o')
                          ->join('yc_user u','o.user_id=u.uid','left')
                          ->where(function ($query)use($where){
                              $query->where($where);
                          })
                         ->whereOr(function($query)use($whereOr){
                             $query->where($whereOr);
                         })
                         ->field('o.order_id,o.order_pay_sn,o.pay_type,o.order_sn,o.order_pay_amount,o.order_status,o.send_time,o.add_time,u.user_name')
                         ->order('o.order_id','desc')
                         ->page($pageNo,$pageSize)
                         ->select();
      $total = $this->order->alias('o')
          ->join('yc_user u','o.user_id=u.uid','left')
          ->where(function ($query)use($where){
              $query->where($where);
          })
          ->whereOr(function($query)use($whereOr){
              $query->where($whereOr);
          })
          ->order('o.order_id','desc')
          ->count();
      if(!empty($list)){
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['list']=$list;
          $msg['total']=$total;
      }else{
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }
      return $msg;
  }
  /**
   *订单详情
   * return json
   */
 public function ordersDetail(){
     if(empty($this->adminInfo)){
         $msg['message']=checkMsg(LOGIN_ERROR);
         return $msg;
     }
     $order_id = $this->request->post('order_id');
     if(empty($order_id)){
         $msg['message']=checkMsg(CANSHU_ERROR);
         return $msg;
     }
     $count = $this->order->where(['order_id'=>$order_id,'is_delete'=>0])->count();
     if($count<1){
         $msg['message']=checkMsg(ORDER_NOT_EXIT);
         return $msg;
     }
     $info = $this->order->alias('o')
                         ->join('yc_user u','u.uid=o.order_id','left')
                         ->field('o.order_id,o.user_id,o.order_sn,o.order_status,o.shipping_sn,o.receiver,o.phone,o.province,o.city,o.district,o.address,o.add_time,r.user_name,r.phone,r.province,r.city,r.district,r.address')
                         ->where(['o.order_id'=>$order_id])
                         ->find();
      $order_goods = $this->orderGoods->alias('og')
                                      ->join('yc_goods g','g.goods_id=og.goods_id','left')
                                      ->where(['og.order_id'=>$info['order_id']])
                                      ->field('og.goods_num,og.goods_price,og.goods_amount,g.goods_name,g.goods_thumb')
                                      ->select();
      $info['goods_list']=$order_goods;
      if(!empty($info)){
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['info']=$info;
      }else{
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }
      return $msg;
 }
 /**
  *修改订单的收货地址
  * return json
  */
  public function editeOrdersAddress(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $order_id = $this->request->post('order_id');
      $province = $this->request->post('province');
      $city     = $this->request->post('city');
      $district = $this->request->post('district');
      $address  = $this->request->post('address');
      if(empty($order_id) || empty($province) || empty($city) || empty($district) || empty($address)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $data=[
          'province' =>$province,
          'city'     =>$city,
          'district' =>$district,
          'address'  =>$address
      ];
      $res = $this->order->save($data,['order_id'=>$order_id]);
      if($res){
          $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(UPDATE_DATA_FAIL);
      }
      return $msg;
  }
  /**
   *订单确认发货
   * return json
   */
  public function sendOrder(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $order_id = $this->request->post('order_id');
      $shipping_company = $this->request->post('shipping_company');
      $shipping_sn   =$this->request->post('shipping_sn');
      if(empty($order_id) || empty($shipping_company) || empty($shipping_sn)){
         $msg['message']=checkMsg(CANSHU_ERROR);
         return $msg;
      }
      $count = $this->order->where(['order_id'=>$order_id,'order_status'=>2,'is_delete'=>0])->count();
      if($count<1){
          $msg['message']=checkMsg(ORDER_NOT_EXIT);
          return $msg;
      }
      $data=[
          'order_status'=>3,
          'shipping_company'=>$shipping_company,
          'shipping_sn'     =>$shipping_sn,
          'send_time'       =>time(),
          'update_time'     =>time()
      ];
     $res = $this->order->save($data,['order_id'=>$order_id]);
     if($res){
         $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
     }else{
         $msg['message']=checkMsg(UPDATE_DATA_FAIL);
     }
     return $msg;
  }
  /**
   *订单退货退款状态
   * return json
   */
  public function refundOrder(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $order_id = $this->request->post('order_id');
      if(empty($order_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $orderIndo = $this->order->where(['order_id'=>$order_id,'is_delete'=>0])->find();
      if(empty($orderIndo)){
          $msg['message']=checkMsg(ORDER_NOT_EXIT);
          return $msg;
      }
      if($orderIndo['order_status']==1 || $orderIndo['order_status']==6 || $orderIndo['order_status']==7){
           $msg['message']=checkMsg(DENY_OPERATE_ORDER);
           return $msg;
      }
      $data=[
          'order_status' =>7,
          'update_time'  =>time()
      ];
      $res = $this->order->save($data,['order_id'=>$order_id]);
      if($res){
          $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(UPDATE_DATA_FAIL);
      }
      return $msg;
  }
 /**
  *删除订单
  * return json
  */
 public function deleteOrder(){
     if(empty($this->adminInfo)){
         $msg['message']=checkMsg(LOGIN_ERROR);
         return $msg;
     }
     $order_id = $this->request->post('order_id');
     if(empty($order_id)){
        $msg['message']=checkMsg(CANSHU_ERROR);
        return $msg;
     }
     $count = $this->order->where(['order_id'=>$order_id,'is_delete'=>0])->count();
      if($count<1){
          $msg['message']=checkMsg(ORDER_NOT_EXIT);
          return $msg;
      }
      $res = $this->order->save(['is_delete'=>1],['order_id'=>$order_id]);
      if($res){
          $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(DELETE_DATA_FAIL);
      }
      return $msg;
 }
}